/*   1:    */ package nl.siegmann.epublib.viewer;
/*   2:    */ 
/*   3:    */ import java.awt.GridLayout;
/*   4:    */ import java.awt.event.MouseAdapter;
/*   5:    */ import java.awt.event.MouseEvent;
/*   6:    */ import java.util.ArrayList;
/*   7:    */ import java.util.Collection;
/*   8:    */ import java.util.HashMap;
/*   9:    */ import java.util.List;
/*  10:    */ import java.util.Map;
/*  11:    */ import javax.swing.JPanel;
/*  12:    */ import javax.swing.JScrollPane;
/*  13:    */ import javax.swing.JTree;
/*  14:    */ import javax.swing.JViewport;
/*  15:    */ import javax.swing.tree.DefaultMutableTreeNode;
/*  16:    */ import javax.swing.tree.TreeNode;
/*  17:    */ import javax.swing.tree.TreePath;
/*  18:    */ import javax.swing.tree.TreeSelectionModel;
/*  19:    */ import nl.siegmann.epublib.browsersupport.NavigationEvent;
/*  20:    */ import nl.siegmann.epublib.browsersupport.NavigationEventListener;
/*  21:    */ import nl.siegmann.epublib.browsersupport.Navigator;
/*  22:    */ import nl.siegmann.epublib.domain.Book;
/*  23:    */ import nl.siegmann.epublib.domain.Resource;
/*  24:    */ import nl.siegmann.epublib.domain.TOCReference;
/*  25:    */ import nl.siegmann.epublib.domain.TableOfContents;
/*  26:    */ import org.apache.commons.lang.StringUtils;
/*  27:    */ 
/*  28:    */ public class TableOfContentsPane
/*  29:    */   extends JPanel
/*  30:    */   implements NavigationEventListener
/*  31:    */ {
/*  32:    */   private static final long serialVersionUID = 2277717264176049700L;
/*  33: 39 */   private Map<String, Collection<DefaultMutableTreeNode>> href2treeNode = new HashMap();
/*  34:    */   private JScrollPane scrollPane;
/*  35:    */   private Navigator navigator;
/*  36:    */   private JTree tree;
/*  37:    */   
/*  38:    */   public TableOfContentsPane(Navigator navigator)
/*  39:    */   {
/*  40: 52 */     super(new GridLayout(1, 0));
/*  41: 53 */     this.navigator = navigator;
/*  42: 54 */     navigator.addNavigationEventListener(this);
/*  43:    */     
/*  44: 56 */     this.scrollPane = new JScrollPane();
/*  45: 57 */     add(this.scrollPane);
/*  46: 58 */     initBook(navigator.getBook());
/*  47:    */   }
/*  48:    */   
/*  49:    */   private static class TOCItem
/*  50:    */   {
/*  51:    */     private TOCReference tocReference;
/*  52:    */     
/*  53:    */     public TOCItem(TOCReference tocReference)
/*  54:    */     {
/*  55: 72 */       this.tocReference = tocReference;
/*  56:    */     }
/*  57:    */     
/*  58:    */     public TOCReference getTOCReference()
/*  59:    */     {
/*  60: 76 */       return this.tocReference;
/*  61:    */     }
/*  62:    */     
/*  63:    */     public String toString()
/*  64:    */     {
/*  65: 80 */       return this.tocReference.getTitle();
/*  66:    */     }
/*  67:    */   }
/*  68:    */   
/*  69:    */   private void addToHref2TreeNode(Resource resource, DefaultMutableTreeNode treeNode)
/*  70:    */   {
/*  71: 85 */     if ((resource == null) || (StringUtils.isBlank(resource.getHref()))) {
/*  72: 86 */       return;
/*  73:    */     }
/*  74: 88 */     Collection<DefaultMutableTreeNode> treeNodes = (Collection)this.href2treeNode.get(resource.getHref());
/*  75: 89 */     if (treeNodes == null)
/*  76:    */     {
/*  77: 90 */       treeNodes = new ArrayList();
/*  78: 91 */       this.href2treeNode.put(resource.getHref(), treeNodes);
/*  79:    */     }
/*  80: 93 */     treeNodes.add(treeNode);
/*  81:    */   }
/*  82:    */   
/*  83:    */   private DefaultMutableTreeNode createTree(Book book)
/*  84:    */   {
/*  85: 97 */     TOCItem rootTOCItem = new TOCItem(new TOCReference(book.getTitle(), book.getCoverPage()));
/*  86: 98 */     DefaultMutableTreeNode top = new DefaultMutableTreeNode(rootTOCItem);
/*  87: 99 */     addToHref2TreeNode(book.getCoverPage(), top);
/*  88:100 */     createNodes(top, book);
/*  89:101 */     return top;
/*  90:    */   }
/*  91:    */   
/*  92:    */   private void createNodes(DefaultMutableTreeNode top, Book book)
/*  93:    */   {
/*  94:105 */     addNodesToParent(top, book.getTableOfContents().getTocReferences());
/*  95:    */   }
/*  96:    */   
/*  97:    */   private void addNodesToParent(DefaultMutableTreeNode parent, List<TOCReference> tocReferences)
/*  98:    */   {
/*  99:109 */     if (tocReferences == null) {
/* 100:110 */       return;
/* 101:    */     }
/* 102:112 */     for (TOCReference tocReference : tocReferences)
/* 103:    */     {
/* 104:113 */       TOCItem tocItem = new TOCItem(tocReference);
/* 105:114 */       DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode(tocItem);
/* 106:115 */       addToHref2TreeNode(tocReference.getResource(), treeNode);
/* 107:116 */       addNodesToParent(treeNode, tocReference.getChildren());
/* 108:117 */       parent.add(treeNode);
/* 109:    */     }
/* 110:    */   }
/* 111:    */   
/* 112:    */   public void navigationPerformed(NavigationEvent navigationEvent)
/* 113:    */   {
/* 114:124 */     if (this == navigationEvent.getSource()) {
/* 115:125 */       return;
/* 116:    */     }
/* 117:127 */     if (navigationEvent.isBookChanged())
/* 118:    */     {
/* 119:128 */       initBook(navigationEvent.getCurrentBook());
/* 120:129 */       return;
/* 121:    */     }
/* 122:131 */     if (this.tree == null) {
/* 123:132 */       return;
/* 124:    */     }
/* 125:134 */     if (navigationEvent.getCurrentResource() == null) {
/* 126:135 */       return;
/* 127:    */     }
/* 128:137 */     Collection<DefaultMutableTreeNode> treeNodes = (Collection)this.href2treeNode.get(navigationEvent.getCurrentResource().getHref());
/* 129:138 */     if ((treeNodes == null) || (treeNodes.isEmpty()))
/* 130:    */     {
/* 131:139 */       if (navigationEvent.getCurrentSpinePos() == navigationEvent.getOldSpinePos() + 1) {
/* 132:140 */         return;
/* 133:    */       }
/* 134:142 */       this.tree.setSelectionPath(null);
/* 135:143 */       return;
/* 136:    */     }
/* 137:145 */     for (DefaultMutableTreeNode treeNode : treeNodes)
/* 138:    */     {
/* 139:146 */       TreeNode[] path = treeNode.getPath();
/* 140:147 */       TreePath treePath = new TreePath(path);
/* 141:148 */       this.tree.setSelectionPath(treePath);
/* 142:    */     }
/* 143:    */   }
/* 144:    */   
/* 145:    */   private void initBook(Book book)
/* 146:    */   {
/* 147:153 */     if (book == null) {
/* 148:154 */       return;
/* 149:    */     }
/* 150:156 */     this.tree = new JTree(createTree(book));
/* 151:157 */     this.tree.addMouseListener(new MouseAdapter()
/* 152:    */     {
/* 153:    */       public void mouseClicked(MouseEvent me)
/* 154:    */       {
/* 155:160 */         DefaultMutableTreeNode node = (DefaultMutableTreeNode)TableOfContentsPane.this.tree.getLastSelectedPathComponent();
/* 156:161 */         TableOfContentsPane.TOCItem tocItem = (TableOfContentsPane.TOCItem)node.getUserObject();
/* 157:162 */         TableOfContentsPane.this.navigator.gotoResource(tocItem.getTOCReference().getResource(), tocItem.getTOCReference().getFragmentId(), TableOfContentsPane.this);
/* 158:    */       }
/* 159:165 */     });
/* 160:166 */     this.tree.getSelectionModel().setSelectionMode(1);
/* 161:    */     
/* 162:168 */     this.tree.setSelectionRow(0);
/* 163:169 */     this.scrollPane.getViewport().removeAll();
/* 164:170 */     this.scrollPane.getViewport().add(this.tree);
/* 165:    */   }
/* 166:    */ }


/* Location:           C:\Users\David\workspace\CosaRaraPorqueSi\main\epublib-tools-3.1.jar
 * Qualified Name:     nl.siegmann.epublib.viewer.TableOfContentsPane
 * JD-Core Version:    0.7.0.1
 */